package com.electric.action;

import java.util.HashMap;

import com.electric.base.BaseAction;
import com.electric.model.Company;
import com.electric.service.CompanyService;
import com.google.gson.FieldNamingPolicy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.opensymphony.xwork2.Action;

/**
 * @author Kevin Zhang
 *
 * 2011-5-24
 */
public class CompanyAction extends BaseAction{
	private CompanyService companyService = new CompanyService();
	
	// 用户Ajax返回数据   
    private String result;
    
    private int up_id ;
	private String name ;
	private String key_man;
	private String address ;
	private String phone_no;
	private String website ;
	private String major_busi;
	private String level ;
	private String lng;
    private String lat;
    
//    private HashMap<Integer, String> companyMap = new HashMap<Integer, String>(); 
    
    private String options;

	public String getOptions() {
		return options;
	}

	public void setOptions(String options) {
		this.options = options;
	}

	@Override
	public String execute() throws Exception {
		
		return Action.SUCCESS;
	}
	
	public String loadByLatLng(){
		Company c = companyService.loadByLatLng(lng, lat);
		
		Gson gson = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_DASHES).create();
		
		setResult(gson.toJson(c));
		
		HashMap<Integer, String> cmap = companyService.getComList();
		
		setOptions(AssembleSelectHtml(cmap));
		
		return Action.SUCCESS;
	}
	
	public String add(){
		Company c = new Company();
		c.setUp_id(up_id);
		c.setName(name);
		c.setKey_man(key_man);
		c.setAddress(address);
		c.setPhone_no(phone_no);
		c.setWebsite(website);
		c.setMajor_busi(major_busi);
		c.setLevel(level);
		c.setLongitude(lng);
		c.setLatitude(lat);
		
		companyService.add(c);
		
		setResult(Action.SUCCESS);
		
		return Action.SUCCESS;
	}
	
	public String loadAllInfos(){
		
		Gson gson = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_DASHES).create();
		setResult(gson.toJson(companyService.loadAllInfos()));
		
		return Action.SUCCESS;
	}
	
	public String loadAllInfosByName(){
		
		Gson gson = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_DASHES).create();
		setResult(gson.toJson(companyService.loadAllInfosByName(name)));
		
		return Action.SUCCESS;
	}
	
	public String update(){
		//取到数据
		Company c = companyService.loadByLatLng(lng, lat);
		c.setName(name);
		c.setAddress(address);
		c.setPhone_no(phone_no);
		companyService.update(c);
		
		setResult(Action.SUCCESS);
		
		return Action.SUCCESS;
	}
	
	public String delete(){
		//取到数据
		Company c = companyService.loadByLatLng(lng, lat);
		companyService.delete(c);
		setResult(Action.SUCCESS);
		
		return Action.SUCCESS;
	}
	
	private String AssembleSelectHtml(HashMap<Integer, String> cmap) {
		StringBuilder sb = new StringBuilder("<option value ="+0+">无</option>");
		for (Object key : cmap.keySet()) {
			sb.append("<option value ="+key+">"+cmap.get(key)+"</option>");
		}
		return sb.toString();
	}
	
	public String getResult() {
		return result;
	}
	public void setResult(String result) {
		this.result = result;
	}

	public String getLng() {
		return lng;
	}

	public void setLng(String lng) {
		this.lng = lng;
	}

	public String getLat() {
		return lat;
	}

	public void setLat(String lat) {
		this.lat = lat;
	}
	
	public int getUp_id() {
		return up_id;
	}

	public void setUp_id(int up_id) {
		this.up_id = up_id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getKey_man() {
		return key_man;
	}

	public void setKey_man(String key_man) {
		this.key_man = key_man;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getPhone_no() {
		return phone_no;
	}

	public void setPhone_no(String phone_no) {
		this.phone_no = phone_no;
	}

	public String getWebsite() {
		return website;
	}

	public void setWebsite(String website) {
		this.website = website;
	}

	public String getMajor_busi() {
		return major_busi;
	}

	public void setMajor_busi(String major_busi) {
		this.major_busi = major_busi;
	}

	public String getLevel() {
		return level;
	}

	public void setLevel(String level) {
		this.level = level;
	}
}
